//microcommand define name
userFields(["name", "authority", "permissions"])
alias("callme")
signature("[name:rawtext]", { checkArgCount: true, strictOptions: true })
option("user", "-u <user:user>", { descPath: "admin.user-option" })
var cmdName = name
ctx.permissions.define("command:" + cmdName + ":set-other-user", {
  inherits: ["authority:3"],
})
action(async ({ options, session }, name) => {
  let user, platform, pid
  if (options.user) {
    platform = options.user.split(":")[0]
    pid = options.user.slice(platform.length + 1)
    user = await ctx.database.getUser(platform, pid)
    if (!user) return session.text("admin.user-not-found")
  } else {
    user = session.user
  }
  const isSelf = !options.user || user?.id === session.user?.id
  if (!name) {
    if (user?.name)
      return session.text(isSelf ? ".your-name" : ".their-name", [user.name])
    return session.text(isSelf ? ".your-name-null" : ".their-name-null")
  }
  if (
    await ctx.permissions.test("command:" + cmdName + ":set-other-user", session)
  ) {
    let modified
    if (isSelf) {
      modified = session.user.name !== name
      if (modified) session.user.name = name
    } else {
      ;({ modified } = await ctx.database.setUser(platform, pid, { name }))
    }
    return session.text(
      modified ? "admin.user-updated" : "admin.user-unchanged"
    )
  } else if (!isSelf) {
    return session.text("internal.low-authority")
  }
  if (session.user.name) return session.text(".already-set")
  if (!name.match(/^[-_0-9A-Za-z\u4e00-\u9fa5]{2,18}$/)) return session.text(".invalid-name")
  await session.send(session.text(".confirm", [name]))
  if (!["Y", "y"].includes((await session.prompt()).trim()))
    return session.text(".canceled")
  session.user.name = name
  return session.text(".updated")
})
locale("zh-CN", {
  description: "查看或设置短昵称",
  messages: {
    "your-name": "你的短昵称：{0}",
    "their-name": "此人的短昵称：{0}",
    "your-name-null": "你未设置短昵称。",
    "their-name-null": "此人未设置短昵称。",
    "invalid-name": "该短昵称无效；只能包含英文字母、阿拉伯数字、横杠、下划线、基本汉字，不可少于 2 个或多于 18 个字符。",
    confirm: "确定设置你的短昵称为 {0} 吗？你只能自行设置一次！回复 Y 来确认。",
    canceled: "已取消操作。",
    updated: "设置成功。",
    "already-set": "你已设置过短昵称，若要修改，请联系 bot 管理员。",
  },
})